package com.kanshu.ksgb.fastread.doudou.module.book.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.dl7.recycler.adapter.BaseViewHolder;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.SelectedBookInfo;
import com.kanshu.ksgb.fastread.doudou.module.book.view.AdBookDetailsOneLayout;
import com.kanshu.ksgb.fastread.doudou.module.book.view.AdBookDetailsTwoLayout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.DefaultItemLayout;

import java.util.List;

/**
 * 书籍详情适配器
 * Created by wcy on 18/5/27.
 */

public class BookDetailsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {
    public static final int HEADER_VIEW = 0x00000111;
    public static final int FOOTER_VIEW = 0x00000112;
    private List<SelectedBookInfo> mDatas;
    protected View mHeaderView;
    protected View mFooterView;

    public BookDetailsAdapter(List<SelectedBookInfo> list) {
        mDatas = list;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder;
        switch (viewType) {
            case HEADER_VIEW:
                baseViewHolder = new BaseViewHolder(mHeaderView);
                break;
            case FOOTER_VIEW:
                baseViewHolder = new BaseViewHolder(mFooterView);
                break;
            case SelectedBookInfo.TYPE_ONE:
                baseViewHolder = new BaseViewHolder(wrapItemView(new AdBookDetailsOneLayout(parent.getContext())));
                break;
            case SelectedBookInfo.TYPE_TWO:
                baseViewHolder = new BaseViewHolder(wrapItemView(new AdBookDetailsTwoLayout(parent.getContext())));
                break;
            default:
                baseViewHolder = new BaseViewHolder(wrapItemView(new DefaultItemLayout(parent.getContext())));
                break;
        }
        return baseViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        BaseViewHolder baseViewHolder = (BaseViewHolder) holder;
        View view = baseViewHolder.getConvertView();
        switch (getItemViewType(position)) {
            case HEADER_VIEW:
                break;
            case FOOTER_VIEW:
                break;
            case SelectedBean.TYPE_ONE:
                if (view instanceof AdBookDetailsOneLayout) {
                    ((AdBookDetailsOneLayout) view).refresh(mDatas.get(position - getHeaderViewsCount()).list);
                }
                break;
            case SelectedBean.TYPE_TWO:
                if (view instanceof AdBookDetailsTwoLayout) {
                    ((AdBookDetailsTwoLayout) view).refresh(mDatas.get(position - getHeaderViewsCount()).list);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size() + getHeaderViewsCount() + getFooterViewsCount();
    }
    public int getHeaderViewsCount() {
        return mHeaderView == null ? 0 : 1;
    }
    public int getFooterViewsCount() {
        return mFooterView == null ? 0 : 1;
    }
    public View getHeaderView() {
        return mHeaderView;
    }
    public void addHeaderView(View headerView) {
        headerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        mHeaderView = headerView;
        notifyItemChanged(0);
    }
    public View getFooterView() {
        return mFooterView;
    }
    public void addFooterView(View footerView) {
        footerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        mFooterView = footerView;
        notifyItemChanged(0);
    }

    private ViewGroup wrapItemView(ViewGroup viewGroup) {
        viewGroup.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        return viewGroup;

    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView != null && position == 0) {
            return HEADER_VIEW;
        }
        if (mFooterView != null && position == getItemCount() - 1) {
            return FOOTER_VIEW;
        }
        if (position - getHeaderViewsCount() < 0) {
            return 0;
        }
        return mDatas.get(position - getHeaderViewsCount()).type;
    }
}
